package com.tang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tang.dto.DocDTO;
import com.tang.dto.DocDTO;
import com.tang.mapper.BookMapper;
import com.tang.mapper.ContentMapper;
import com.tang.mapper.DocMapper;
import com.tang.util.DeepBeanUtils;
import com.tang.util.RedisUtil;
import com.tang.util.RequestContext;
import com.tang.vo.Content;
import com.tang.vo.Doc;
import com.tang.vo.Doc;
import com.tang.service.DocService;
import com.tang.mapper.DocMapper;
import com.tang.websocket.WebSocketServer;
import org.apache.commons.lang3.ObjectUtils;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author tang
* @description 针对表【doc(文档)】的数据库操作Service实现
* @createDate 2023-04-29 21:17:23
*/
@Service
public class DocServiceImpl implements DocService{
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private DocMapper docMapper;
    @Autowired
    private ContentMapper contentMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private WsService wsService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public List<DocDTO> all() {
        QueryWrapper<Doc> wrapper=new QueryWrapper<>();
        List<Doc> doc = docMapper.selectList(wrapper);
        return DeepBeanUtils.copyListProperties(doc, DocDTO::new);
    }

    @Override
    public Map<String, Object> list(String name,String ebookId,int pageNum, int pageSize) {
        QueryWrapper<Doc> wrapper=new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(name)){
            wrapper.like("name",name);
        }
        if (ObjectUtils.isNotEmpty(ebookId)){
            wrapper.eq("ebook_id",ebookId);
        }
        wrapper.eq("parent",0).orderByAsc("sort");
        Map<String,Object> map=new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Doc> docs = docMapper.selectList(wrapper);
        PageInfo<Doc> pageInfo=new PageInfo<>(docs);
        List<DocDTO> DocDTOS = DeepBeanUtils.copyListProperties(docs, DocDTO::new);
        map.put("data",DocDTOS);
        map.put("total",pageInfo.getTotal());
        map.put("pages",pageInfo.getPages());
        return map;
    }

    @Override
    public List<DocDTO> listChildren(String id) {
        QueryWrapper<Doc> wrapper=new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(id)){
            wrapper.eq("parent",id).orderByAsc("sort");
            List<Doc> list = docMapper.selectList(wrapper);
            return DeepBeanUtils.copyListProperties(list, DocDTO::new);
        }
        return null;
    }

    @Override
    public List<DocDTO> listByEbookId(String ebookId) {
        QueryWrapper<Doc> wrapper=new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(ebookId)){
            wrapper.eq("ebook_id",ebookId).orderByAsc("sort");
            int i = bookMapper.updateViewCount(ebookId);
            if (i>0){
                List<Doc> list = docMapper.selectList(wrapper);
                return DeepBeanUtils.copyListProperties(list, DocDTO::new);
            }
        }
        return null;
    }

    @Override
    public String oneContent(String id) {
        Content content = contentMapper.selectById(id);
        int i = docMapper.updateViewCount(id);
        if (ObjectUtils.isNotEmpty(content)&&i>0){
            return content.getContent();
        }
        return "";
    }

    @Override
    public int update(DocDTO docDTO) {
        Doc doc = new Doc();
        Content content = new Content();
        BeanUtils.copyProperties(docDTO,doc);
        BeanUtils.copyProperties(docDTO,content);
        if (ObjectUtils.isNotEmpty(docDTO.getId())&&!docDTO.getId().equals("0")){
            System.out.println(content.getContent());
            if (ObjectUtils.isNotEmpty(content.getContent())){
                int i = contentMapper.updateById(content);

                if (i==0){
                    contentMapper.insert(content);
                }
            }
            return docMapper.updateById(doc);
        }
        doc.setId(null);
        int i = docMapper.insert(doc);
        if (i>0){
            content.setId(doc.getId());
            if (ObjectUtils.isNotEmpty(content.getContent())){
                return contentMapper.insert(content);
            }
            return i;
        }
        return 0;
    }

    @Override
    public int updateVote(String id) {
        if (ObjectUtils.isNotEmpty(id)){
            String key = RequestContext.getRemoteAddr();
            if (redisUtil.validateRepeat("DOC_VOTE_"+id+"_"+key,3600*24)){
                Doc doc = docMapper.selectById(id);
                if (ObjectUtils.isNotEmpty(doc)){
//                    rabbitTemplate.convertAndSend("vote","【"+doc.getName()+"】被点赞！！！");
                    wsService.sendInfo("【"+doc.getName()+"】被点赞！！！");
                    return docMapper.updateVoteCount(id);
                }
            }
        }
        return 0;
    }

    @Override
    public int delete(String id) {
        if (ObjectUtils.isNotEmpty(id)){
            contentMapper.deleteById(id);
            return docMapper.deleteById(id);
        }
        return 0;
    }

    @Override
    public int deleteBatch(List<String> ids) {
        if (ObjectUtils.isNotEmpty(ids)){
            return docMapper.deleteBatchIds(ids);
        }
        return 0;
    }
}




